package com.sitech.openapi.service;

import com.sitech.openapi.dto.ProdPreOrder;
import com.sitech.openapi.dto.ProdPreOrderExample;
import com.sitech.openapi.dto.ProdPreOrderHis;
import com.sitech.openapi.mapper.ProdPreOrderHisMapper;
import com.sitech.openapi.mapper.ProdPreOrderMapper;
import com.sitech.openapi.mapper.ProdPreOrderMapperService;
import com.sitech.openapi.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

/**
 * Created by zhangsq on 2018/12/7.
 * 修改产品订单：
 */
@Service
@Slf4j
public class ProductOrderServiceImpl implements ProductOrderService {
    @Autowired
    private ProdPreOrderMapper prodPreOrderMapper;

    @Autowired
    private ProdPreOrderHisMapper prodPreOrderHisMapper;

    @Autowired
    private ProdPreOrderMapperService prodPreOrderMapperServcie;

    public Map updateProductOrder(Map inMap) throws Exception {
        Map retMap = new HashMap();
        try {
            String custOrderId = StringUtil.obj2Str(inMap.get("custOrderId"));
            String staffCode = "chedan";
            LinkedList staffCodeList = (LinkedList) inMap.get("staffCode");
            if (staffCodeList != null && staffCodeList.size() > 0) {
                staffCode = StringUtil.obj2Str(staffCodeList.getLast());
            }
            Date currentDate = prodPreOrderMapperServcie.queryCurrentDate();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
            String dateString = formatter.format(currentDate);

            ProdPreOrder prodPreOrder = new ProdPreOrder();
            prodPreOrder.setOperType("1200");
            prodPreOrder.setStatusCd("401700");
            ProdPreOrderExample prodPreOrderExample = new ProdPreOrderExample();
            prodPreOrderExample.createCriteria().andCustOrderIdEqualTo(new BigDecimal(custOrderId));
            prodPreOrderMapper.updateByExampleSelective(prodPreOrder, prodPreOrderExample);

            //更新历史表内的记录
            List<ProdPreOrder> prodPreOrderList = prodPreOrderMapper.selectByExample(prodPreOrderExample);
            for (int i = 0; i < prodPreOrderList.size(); i++) {
                ProdPreOrder prodPreOrder1 = prodPreOrderList.get(i);
                ProdPreOrderHis prodPreOrderHis = new ProdPreOrderHis();
                PropertyUtils.copyProperties(prodPreOrderHis, prodPreOrder1);
                prodPreOrderHis.setHisDate(Long.parseLong(dateString));
                prodPreOrderHis.setHisLogin(staffCode);
                prodPreOrderHis.setHisTime(currentDate);
                prodPreOrderHis.setHisType("U");
                prodPreOrderHis.setHisAccept(new BigDecimal(custOrderId));
                prodPreOrderHisMapper.insert(prodPreOrderHis);
            }
            if (prodPreOrderList != null && prodPreOrderList.size() > 0) {
                ProdPreOrder prodPreOrder1 = prodPreOrderList.get(0);
                Date accpetDate = prodPreOrder1.getAcceptDate();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                String acceptDates = format.format(accpetDate);
                retMap.put("acceptDate", acceptDates);
                retMap.put("acceptRegionId", "8" + prodPreOrder1.getAcceptRegionId() + "00");
                retMap.put("createStaff", prodPreOrder1.getCreateStaff());
                retMap.put("custOrderId", prodPreOrder1.getCustOrderId());
                retMap.put("extCustOrderId", prodPreOrder1.getExtCustOrderId());
                retMap.put("operType", prodPreOrder1.getOperType());
                retMap.put("remark", prodPreOrder1.getRemark());
                retMap.put("statusCd", prodPreOrder1.getStatusCd());
                retMap.put("sysSource", prodPreOrder1.getSysSource());
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info(e.getMessage());
            throw new Exception(e.getMessage());
        }

        return retMap;
    }
}
